﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango.Protocol.party;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Environment;
using System.Data;

namespace Pixysoft.Weblications.Sango.Protocol
{
    class PartyFacade
    {
        public static party_party ToParty(Entities.Usr_Party orm)
        {
            party_party body = ReflectionManager.CreatePojo<party_party>();
            body.partycode = orm.Primarycode;
            body.name = orm.Partyname;
            body.logo = orm.Logoname;
            body.logolevel = orm.Logolevel;
            body.level = orm.Partylevel;
            body.count = orm.Membercount;
            body.totalcount = orm.Maxmembercount;

            body.comment = orm.Description;
            body.createuser = orm.Precidentusername;
            body.createusercode = orm.Precidentusercode;
            body.createdate = (long)orm.Initializedate;

            body.nation = SysNationHelper.search(orm.Nationtypecode).Nationname;

            return body;
        }

        public static List<party_party> ToParties(List<Entities.Usr_Party> orms)
        {
            List<party_party> list = new List<party_party>();
            foreach (Entities.Usr_Party orm in orms)
            {
                list.Add(ToParty(orm));
            }
            return list;
        }

        internal static party_member ToPartyMember(Entities.Usr_Profile profile, Entities.Usr_Party party, Entities.Usr_Partymember orm)
        {
            Entities.Pty_Partytitle title = PtyPartyTitleHelper.searchByCode(orm.Titletypecode);

            party_member body = ReflectionManager.CreatePojo<party_member>();

            body.partycode = party.Primarycode;
            body.partyname = party.Partyname;
            body.partylevel = party.Partylevel;

            body.usercode = orm.Usercode;
            body.nickname = orm.Nickname;
            body.playerlevel = profile.Userlevel;

            body.titletypecode = title.Titletypecode;
            body.titletypename = title.Titlename;
            body.contribution = orm.Partycontributionvalue;

            return body;
        }

        public static List<party_listmember> ToApplyMembers(DataTable table)
        {
            List<party_listmember> list = new List<party_listmember>();

            foreach (DataRow row in table.Rows)
            {
                long sectionserialcode = long.Parse(row[Schemas.LOCAL.USR_SECTION.SECTIONSERIALCODE.Column].ToString());
                long nationtypecode = long.Parse(row[Schemas.LOCAL.USR_PROFILE.NATIONTYPECODE.Column].ToString());

                Entities.Sys_Section sysSection = SysSectionHelper.searchSectionByCode(sectionserialcode);
                Entities.Sys_Nation sysNation = SysNationHelper.search(nationtypecode);

                party_listmember orm = ReflectionManager.CreatePojo<party_listmember>();
                orm.usercode = row[Schemas.LOCAL.USR_PROFILE.USERCODE.Column].ToString();
                orm.nickname = row[Schemas.LOCAL.USR_PROFILE.NICKNAME.Column].ToString();
                orm.playerlevel = long.Parse(row[Schemas.LOCAL.USR_PROFILE.USERLEVEL.Column].ToString());
                orm.avatar = long.Parse(row[Schemas.LOCAL.USR_PROFILE.AVATARTYPECODE.Column].ToString());

                orm.nation = sysNation.Nationname;
                orm.nationtypecode = sysNation.Nationtypecode;
                orm.section = sysSection.Sectionname;
                orm.sectionserialcode = sysSection.Sectionserialcode;

                orm.partycode = row[Schemas.LOCAL.USR_PARTYMEMBER.PARTYPRIMARYCODE.Column].ToString();
                orm.partyname = row[Schemas.LOCAL.USR_PARTYMEMBER.PARTYNAME.Column].ToString();
                orm.contribution = long.Parse(row[Schemas.LOCAL.USR_ACTIVITY.PARTYMISSIONDONATEVALUE.Column].ToString());

                list.Add(orm);
            }

            return list;
        }

        public static List<party_listmember> ToListMembers(DataTable table)
        {
            List<party_listmember> list = new List<party_listmember>();

            foreach (DataRow row in table.Rows)
            {
                long sectionserialcode = long.Parse(row[Schemas.LOCAL.USR_SECTION.SECTIONSERIALCODE.Column].ToString());
                long nationtypecode = long.Parse(row[Schemas.LOCAL.USR_PROFILE.NATIONTYPECODE.Column].ToString());
                long titletypecode = long.Parse(row[Schemas.LOCAL.USR_PARTYMEMBER.TITLETYPECODE.Column].ToString());

                Entities.Sys_Section sysSection = SysSectionHelper.searchSectionByCode(sectionserialcode);
                Entities.Sys_Nation sysNation = SysNationHelper.search(nationtypecode);
                Entities.Pty_Partytitle sysTitle = PtyPartyTitleHelper.searchByCode(titletypecode);

                party_listmember orm = ReflectionManager.CreatePojo<party_listmember>();
                orm.usercode = row[Schemas.LOCAL.USR_PROFILE.USERCODE.Column].ToString();
                orm.nickname = row[Schemas.LOCAL.USR_PROFILE.NICKNAME.Column].ToString();
                orm.playerlevel = long.Parse(row[Schemas.LOCAL.USR_PROFILE.USERLEVEL.Column].ToString());
                orm.avatar = long.Parse(row[Schemas.LOCAL.USR_PROFILE.AVATARTYPECODE.Column].ToString());

                orm.nation = sysNation.Nationname;
                orm.nationtypecode = sysNation.Nationtypecode;
                orm.section = sysSection.Sectionname;
                orm.sectionserialcode = sysSection.Sectionserialcode;

                orm.partycode = row[Schemas.LOCAL.USR_PARTYMEMBER.PARTYPRIMARYCODE.Column].ToString();
                orm.partyname = row[Schemas.LOCAL.USR_PARTYMEMBER.PARTYNAME.Column].ToString();
                orm.titletypecode = titletypecode;
                orm.titletypename = sysTitle.Titlename;
                orm.contribution = long.Parse(row[Schemas.LOCAL.USR_ACTIVITY.PARTYMISSIONDONATEVALUE.Column].ToString());
                list.Add(orm);
            }

            return list;
        }

        internal static List<party_mission> toMissions(List<Entities.Usr_Partymission> orms)
        {
            List<party_mission> list = new List<party_mission>();

            foreach (Entities.Usr_Partymission orm in orms)
            {
                party_mission body = ReflectionManager.CreatePojo<party_mission>();
                body.description = orm.Description;
                body.expvalue = orm.Expvalue;
                body.upgradeexpvalue = orm.Upgradeexpvalue;
                body.missiontypecode = orm.Missiontypecode;
                body.name = orm.Missiontypename;
                body.level = orm.Missionlevel;
                list.Add(body);
            }

            return list;
        }

    }
}
